﻿using CandidateManager.Areas.Coach.Models;
using System;
using System.Linq;
using System.Web.Mvc;
using WI.CandidateManager.Business.Objects;
using CommonEntities = WI.CandidateManager.Common.Data.Entities;

namespace CandidateManager.Areas.Coach.Controllers
{
    /// <summary>
    /// Controller for working with groups.
    /// </summary>
    public class GroupsController : CandidateManager.Controllers.AutorizeBaseController
    {
        private GroupBO groupBO;

        private CoachBO coachBO;

        public GroupsController()
            : base()
        {
            groupBO = new GroupBO();
            coachBO = new CoachBO();
        }

        /// <summary>
        /// Get list of coach's groups.
        /// </summary>
        /// <returns>View with list of coach's groups.</returns>
        [Authorize]
        public ActionResult List()
        {
            return View(coachBO.GetGroups());
        }

        /// <summary>
        /// Manages the specified study group id.
        /// </summary>
        /// <param name="studyGroupId">The study group id.</param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public ActionResult Manage(long? studyGroupId = null)
        {
            var groupModifyModel = new GroupModifyModel()
            {
                CandidatesList = new CandidateBO().GetCandidates(),
                Group = new CommonEntities.StudyGroup()
            };

            if (studyGroupId.HasValue)
            {
                var group = groupBO.GetStudyGroups(studyGroupId).Single();
                groupModifyModel.Group = group;
                groupModifyModel.GroupCandidates = group.Candidate;
            }

            return View(groupModifyModel);
        }

        /// <summary>
        /// Manages the specified study group.
        /// </summary>
        /// <param name="studyGroup">The study group.</param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public ActionResult Manage(CommonEntities.StudyGroup studyGroup)
        {
            if (coachBO.SaveGroup(studyGroup))
            {
                return RedirectToAction("Manage", new { @studyGroupId = studyGroup.Id });
            }
            else
            {
                return RedirectToAction("List");
            }
        }

        /// <summary>
        /// Adds the candidate.
        /// </summary>
        /// <param name="candidateId">The candidate id.</param>
        /// <param name="groupId">The group id.</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult AddCandidate(long candidateId, long groupId)
        {
            if (groupBO.AddCandidateToGroup(candidateId, groupId))
            {
                return RedirectToAction("Manage", new { @studyGroupId = groupId });
            }
            else
            {
                return RedirectToAction("List");
            }
        }

        /// <summary>
        /// Deletes the candidate.
        /// </summary>
        /// <param name="candidateId">The candidate id.</param>
        /// <param name="groupId">The group id.</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult DeleteCandidate(long candidateId, long groupId)
        {
            if (groupBO.DeleteCandidateFromGroup(candidateId, groupId))
            {
                return RedirectToAction("Manage", new { @studyGroupId = groupId });
            }
            else
            {
                return RedirectToAction("Manage");
            }
        }

        /// <summary>
        /// Deletes the group.
        /// </summary>
        /// <param name="groupId">The group id.</param>
        /// <returns></returns>
        [Authorize]
        public ActionResult DeleteGroup(long groupId)
        {
            if (groupBO.DeleteGroup(groupId))
            {
                return RedirectToAction("List", "Groups");
            }
            else
            {
                return RedirectToAction("List", "Groups");
            }
        }
    }
}
